package ProjectManagement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.*;

import com.spm.pojo.Teacher;
import com.spm.utils.StudentMapperUtil;

@WebServlet(name = "TLoginServlet",value = "/ProjectManagement/TLoginServlet.do")
public class LoginServlet extends HttpServlet {
    private static final long seriaVersionUID=1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");//解决乱码问题

        String username=req.getParameter("username").trim();//获取username并去掉空格
        String password=req.getParameter("password").trim();//获取password并去掉空格
        String imageValue=req.getParameter("imageValue");
        String remember=req.getParameter("remember");
        String loginer=req.getParameter("loginer");
        req.setAttribute("username", username);
        req.setAttribute("password", password);
        req.setAttribute("imageValue", imageValue);
        String sRand=(String) session.getAttribute("sRand");
        PrintWriter out = resp.getWriter();
        if(StringUtil.isEmpty(username)||StringUtil.isEmpty(password)){
            req.getRequestDispatcher("TeacherLogin.jsp").forward(req, resp);
            return;
        }
        if(StringUtil.isEmpty(imageValue)){
            req.setAttribute("error", "请输入验证码！");
            req.getRequestDispatcher("TeacherLogin.jsp").forward(req,resp);
            return;
        }else if(!imageValue.equals(sRand)){
            req.setAttribute("error", "验证码错误！");
            req.getRequestDispatcher("TeacherLogin.jsp").forward(req,resp);
            return;
        }
        if (loginer.equals("teacher")){
            Teacher teacher=new Teacher(username,password);
            TeacherDaoImpl usersdao=new TeacherDaoImpl();
            session.setAttribute("username", username);
            session.setAttribute("teacher",teacher);
            if(usersdao.isLogin(teacher)){
                if("remember-me".equals(remember)){
                    rememberMe(username,password,resp);
                }
                resp.sendRedirect("main.jsp");
            }
            else{
                req.setAttribute("error", "用户名或密码错误!");
                RequestDispatcher requestdispatcher=req.getRequestDispatcher("TeacherLogin.jsp");
                requestdispatcher.forward(req, resp);
            }
        }else if (loginer.equals("student")){
            StudentMapperUtil studentMapperUtil = new StudentMapperUtil();
            if(studentMapperUtil.selectIdAndPassword(username,password)==1){
                session.setAttribute("numSession", username);
                Cookie cookie = new Cookie("stuno",username);
                cookie.setMaxAge(60*60*24*30);
                resp.addCookie(cookie);
                //登录成功，跳转...
                //response.sendRedirect("http://localhost:8080/cjw_war_exploded/Olsystem/logok.jsp");
                RequestDispatcher dispatcher = req.getRequestDispatcher("/FindServlet");
                dispatcher.forward(req,resp);
            }else{
                out.print("登录失败");
                out.print("<br><a href = 'login.jsp'>重新登陆</a>");
//                response.setContentType("text/html;charset=UTF-8");
//
//                response.setHeader("refresh", "0; url=/StudentSelect");

            }

            }


    }
    private void rememberMe(String userName,String password,HttpServletResponse response){
        Cookie user;
        try {
            user = new Cookie("user", URLEncoder.encode(userName,"UTF-8")+"-"+URLEncoder.encode(password,"UTF-8"));
            user.setMaxAge(1*60*60*24*7);
            response.addCookie(user);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
